//
//  GTMediator.m
//  SampleApp2
//
//  Created by GXHC on 2023/6/29.
//

#import "GTMediator.h"
//#import "../RecommendController/Controller/GTDetailViewController.h"
@implementation GTMediator
#pragma mark -target action
+ (__kindof UIViewController *)detailViewControllerWithUrl:(NSString *)detailUrl {
    Class detailCls = NSClassFromString(@"GTDetailViewController");
    UICollectionView *controller = [[detailCls alloc]performSelector:NSSelectorFromString(@"initWithUrlString:") withObject:detailUrl];

//	GTDetailViewController *controller = [[GTDetailViewController alloc]initWithUrlString:detailUrl];
    return controller;
}

#pragma mark - scheme
+ (NSMutableDictionary *)mediatorCache {
    static NSMutableDictionary *cache;
    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{
        cache = @{}.mutableCopy;
    });
    return cache;
}

+ (void)registerScheme:(NSString *)scheme processBlock:(GTMediatorProcessBlock)processBlock {
    if (scheme && processBlock) {
        [[[self class]mediatorCache] setObject:processBlock forKey:scheme];
    }
}

+ (void)openUrl:(NSString *)url params:(NSDictionary *)params {
    GTMediatorProcessBlock block = [[[self class]mediatorCache]objectForKey:url];

    if (block) {
        block(params);
    }
}

#pragma mark - protocol
+ (void)registerProtocol:(Protocol *)proto cls:(Class)cls {
	if(proto && cls){
		[[[self class]mediatorCache] setObject:cls forKey:NSStringFromProtocol(proto)];
	}
	
}

+ (Class)classForProtocol:(Protocol *)proto {
	return [[[self class]mediatorCache]objectForKey:NSStringFromProtocol(proto)];
}

@end
